﻿using Cache;
using DtoFw;
using DtoFw.Base;
using DtoFw.Query;
using DtoFw.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Service;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace SceneEvidenceAPI.Controllers
{
    /// <summary>
    /// 案件
    /// </summary>
    [Route("api/evidence")]
    public class EvidenceController : BaseController
    {
        private readonly EvidenceService _evidenceService;
        public EvidenceController(EvidenceService evidenceService)
        {
            this._evidenceService = evidenceService;
        }
        /// <summary>
        /// 新增物品
        /// </summary>
        /// <param name="reqDto"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResult Post([FromBody] EvidencePostReqDto reqDto)
        {
            return _evidenceService.Post(reqDto);
        }
        /// <summary>
        /// 修改物品
        /// </summary>
        /// <param name="reqDto"></param>
        /// <returns></returns>
        [HttpPut]
        public BaseResult Put([FromBody] EvidencePutReqDto reqDto)
        {
            return _evidenceService.Put(reqDto);
        }
        /// <summary>
        /// 修改物品RFID
        /// </summary>
        /// <param name="reqDtos"></param>
        /// <returns></returns>
        [HttpPut("rfid")]
        public BaseResult PutRfid([FromBody] EvidenceRfidListReqDto reqDto)
        {
            return _evidenceService.PutRFID(reqDto);
        }
        /// <summary>
        /// 更换位置
        /// </summary>
        /// <param name="reqDtos"></param>
        /// <returns></returns>
        [HttpPut("location")]
        public BaseResult PutLocation([FromBody] EvidenceLocationPutReqDto putReqDto)
        {
            return _evidenceService.PutLocation(putReqDto);
        }
        /// <summary>
        /// 删除物品
        /// </summary>
        /// <param name="reqDto"></param>
        /// <returns></returns>
        [HttpDelete]
        public BaseResult Delete([FromBody] DeleteDto reqDto)
        {
            return _evidenceService.Delete(reqDto);
        }
        /// <summary>
        /// 案件物品分页
        /// </summary>
        /// <param name="reqDto"></param>
        /// <returns></returns>
        [HttpGet("pager")]
        public BaseGenericResult<Pager<CaseEvidencePagerRespDto>> QueryCaseEvidences([FromQuery] EvidenceQueryReqDto reqDto)
        {
            return BaseGenericResult<Pager<CaseEvidencePagerRespDto>>.Succeed(
                _evidenceService.QueryCaseEvidences(reqDto));
        }
        /// <summary>
        /// 案件物品分页
        /// </summary>
        /// <param name="reqDto"></param>
        /// <returns></returns>
        [HttpGet("box/pager")]
        public BaseGenericResult<Pager<CaseEvidencePagerRespDto>> QueryCaseEvidencesByLocationId([FromQuery] EvidenceLocationQueryReqDto reqDto)
        {
            return BaseGenericResult<Pager<CaseEvidencePagerRespDto>>.Succeed(
                _evidenceService.QueryCaseEvidencesByLocationId(reqDto));
        }
        /// <summary>
        /// 案件物品分页
        /// </summary>
        /// <param name="reqDto"></param>
        /// <returns></returns>
        [HttpGet("{evidenceId}")]
        public BaseGenericResult<EvidenceRespDto> QueryCaseEvidences(long evidenceId)
        {
            return _evidenceService.Get(evidenceId);
        }
        /// <summary>
        /// 获取案件待借调物品
        /// </summary>
        /// <param name="evidenceId"></param>
        /// <returns></returns>
        [HttpGet("borrow")]
        public BaseGenericResult<List<EvidenceRespDto>> GetCaseBorrowEvdences([FromQuery] CaseEvidenceQueryReqDto reqDto)
        {
            return _evidenceService.GetCaseBorrowEvdences(reqDto);
        }
        /// <summary>
        /// 月度物品流转情况
        /// </summary>
        /// <returns></returns>
        [HttpGet("home")]
        public BaseGenericResult<MonthEvidenceTransfereRespDto> GetMonthEvidenceTransfere()
        {
            return _evidenceService.GetMonthEvidenceTransfere();
        }
        /// <summary>
        /// 获取物品类别数据
        /// </summary>
        /// <returns></returns>
        [HttpGet("home/class")]
        public BaseGenericResult<EvidenceClassRespDto> GetEvidenceClass()
        {
            return _evidenceService.GetEvidenceClass();
        }

        [HttpPost("upload")]
        public BaseGenericResult<string> UploadEvidences(IFormFile f)
        {
            string ossurl = string.Empty;
            var file = Request.Form.Files.First();
            if (file.Length < 1)
                return BaseGenericResult<string>.Failure(ossurl, "参数错误");
            FileInfo fileInfo = new FileInfo(file.FileName);

            long caseID = long.Parse(Request.Form.First(x => x.Key == "id").Value);
            using (var stream = file.OpenReadStream())
            {
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                stream.Seek(0, SeekOrigin.Begin);
                TokenData tokenData = TokenHelper.GetTokenInfo<TokenData>(User);
                return _evidenceService.UploadEvidences(tokenData, fileInfo, bytes, caseID);
            }

        }

    }
}
